package com.bkgy.controller.plateThickAbnormal.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.controller.plateThickAbnormal.entity.TbPltcmAscResult;
import com.bkgy.controller.plateThickAbnormal.entity.tDefectData;
import com.bkgy.controller.plateThickAbnormal.entity.tDefectDataRequst;
import com.bkgy.controller.plateThickAbnormal.mapper.plateThickAbnormalMapper;
import com.bkgy.controller.plateThickAbnormal.service.plateThickAbnormalService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class plateThickAbnormalServiceImpl implements plateThickAbnormalService {

    @Autowired
    plateThickAbnormalMapper plateThickAbnormalMapper;

    /**
     * 酸轧厚度、版型异常明细_分页
     * @param request tDefectDataRequst
     * @return ResultData
     */
    public ResultData getTdefectDataPage(tDefectDataRequst request) {
        //日期转换
        String dbegin_str="";
        if(!StringUtils.isEmpty(request.getDbegin())){
            dbegin_str = constructL3Date(request.getDbegin());
        }
        String dend_str ="";
        if(!StringUtils.isEmpty(request.getDend())){
            dend_str =constructL3Date(request.getDend());
        }
        request.setDbegin(dbegin_str);
        request.setDend(dend_str);

        try {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());
            List<TbPltcmAscResult> list = plateThickAbnormalMapper.getTdefectDataPage(page,request);
            Integer count = Convert.toInt(page.getTotal());
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 将日期格式由YYYY-MM-DD HH:MM:SS 转为 YYYYMMDDHHSSMM
     * @param Date_str YYYY-MM-DD HH:MM:SS
     * @return YYYYMMDDHHSSMM
     */
    public String constructL3Date(String Date_str) {
        String l3Date = "";//时间格式：20250703075254
        try {
            String[] array_Time = Date_str.split("-");//时间格式：2025-06-01 00:00:01
            //构建时间格式：20250703075254
            String times2 = array_Time[2].replace(" ", "");
            times2 = times2.replace(":", "");
            l3Date = array_Time[0] + array_Time[1] + times2;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return l3Date;
    }

  }
